#!/usr/bin/env jruby
# -*- coding: us-ascii -*-
# Elizabeth Wiethoff 2008

require 'java'

########################################

class Array
  def to_vector
    java.util.Vector.new self
  end
  
  def Array.from_vector(vector)
    unless vector.is_a? Java::JavaUtil::AbstractList
      raise TypeError, 'not a vector'
    end
    vector.to_a
  end
  
  # Vector of Vector or AbstractList of AbstractLists
  def to_vov
    unless self[0].respond_to? :to_ary
      raise TypeError, 'array is not an array of arrays'
    end
    map { |a| a.to_vector }.to_vector
  end
  
  # Vector of Vector or AbstractList of AbstractLists
  def Array.from_vov(vov)
    unless vov.is_a? Java::JavaUtil::AbstractList
      raise TypeError, 'not a vector'
    end
    unless vov.to_a[0].is_a? Java::JavaUtil::AbstractList
      raise TypeError, 'not a vector of vectors'
    end
    vov.to_a.map { |v| v.to_a }
  end
end

########################################

if $0 == __FILE__
  require 'pp'
  
  array = %w'foo bar baz'
  aoa = [
    [1, '/Work',     'foo.txt',    '2007-12-20', 12345, 2], 
    [1, '/Projects', 'foobar.txt', '2007-05-13', 54321, 2], 
    [2, '/Projects', 'barbaz.txt', '2007-10-30',   987, 2], 
    [2, '/Work',     'bar.txt',    '2008-01-24',   654, 2], 
  ]
  pp array, aoa
  puts
  
  vector = array.to_vector
  pp vector
  pp Array.from_vector(vector)
  puts
  
  # p array.to_vov
  # p Array.from_vov(vector)
  # puts
  
  vov = aoa.to_vov
  pp vov
  pp Array.from_vov(vov)
  puts
end
